<?php

namespace App\Http\Middleware;

use Closure;
use App\Morefun\Wechat\Service as WechatService;

class WechatRedirectMiddle
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next,WechatService $service)
    {
        $url = $service->makeSilentlyAuthorizedUrl($request->id);
//        $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx2a564a67a378ae52'
//            .'&redirect_uri='
//            .$request->fullUrl()
//            .'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';

        if (!$request->get('code')){
            return redirect($url);
        }

        return $next($request);
    }
}
